<?php
namespace App\Support\Service;

/**
 * @Author:: HuangYin
 * @DateTime: 2017-03-15 21:51
 *
 * @Description:
 */
class PrinterService
{
    /**
     * 打印纸宽
     */
    const WIDTH = 230;

    /**
     * 字体大小
     */
    const FONT_SIZE = 34;

    /**
     * 打印机名
     */
    const PRINTER_NAME = 'Gprinter';

    const LOGO = 'collect/img/logo.bmp';

    const EWM = 'collect/img/ewm.bmp';

    protected $supermarket = '';
    protected $storeName = '';
    protected $address = '';
    protected $tel = '';
    protected $time = '';
    protected $bill = [];
    protected $slogan = '';
    protected $hotTitle = '';
    protected $hotTel = '';
    protected $refund = '';
    protected $voucher = '';
    protected $keeping = '';

    public function __construct($data, $bill)
    {
        foreach ($data as $k => $v) {
            $this->$k = iconv("UTF-8", "GBK", $v);
        }

        $this->bill = $bill;
    }

    public function start()
    {
        $handle = printer_open(self::PRINTER_NAME);
        printer_abort($handle);
        printer_start_doc($handle, "mpd");
        printer_start_page($handle);
        printer_set_option($handle, PRINTER_MODE, "RAW");

        $this->printHeader($handle);
        //$this->printFooter($handle);

        printer_end_page($handle);
        printer_end_doc($handle);
        printer_close($handle);
    }

    public function printHeader($handle)
    {
        $axis = $this->setHeaderAxis();
        $size = $this->setFontSize();
        printer_select_font($handle, $size);
        printer_draw_bmp($handle, self::LOGO, $axis['x'], $axis['y'], 189, 70);
        printer_draw_text($handle, $this->supermarket, 20, $axis['y'] + 60 + self::FONT_SIZE);
        printer_draw_text($handle, $this->storeName, $axis['x'], $axis['y'] + 60 + self::FONT_SIZE * 2);
        printer_delete_font($size);

        printer_select_font($handle, $size);


        printer_draw_text($handle, iconv("UTF-8", "GBK", "地址：重庆市江北区紫荆广场1楼"), 0, $axis['y'] + 70 + self::FONT_SIZE * 3);
        printer_draw_text($handle, iconv("UTF-8", "GBK", "电话：400-000-9011"), 0, $axis['y'] + 60 + self::FONT_SIZE * 4);
        printer_draw_text($handle, iconv("UTF-8", "GBK", "2017年01月16日（星期一）15:54:26"), 0, $axis['y'] + 60 + self::FONT_SIZE * 5);
        printer_draw_text($handle, iconv("UTF-8", "GBK", "名称                                数量      价格"), 0, $axis['y'] + 60 + self::FONT_SIZE * 6);
        printer_delete_font($size);
    }

    public function printMain($handle)
    {
        return false;
    }

    public function printFooter($handle)
    {
        $axis = $this->setFooterAxis();
        $size = $this->setFontSize();
        printer_select_font($handle, $size);
        printer_draw_bmp($handle, self::EWM, $axis['x'], $axis['y'], 200, 200);
        printer_draw_text($handle, iconv("UTF-8", "GBK", "感谢惠顾，欢迎再次光临"), 20, $axis['y'] + 200 + self::FONT_SIZE);
        printer_draw_text($handle, iconv("UTF-8", "GBK", "服务监督热线"), $axis['x'], $axis['y'] + 200 + self::FONT_SIZE * 2);
        printer_draw_text($handle, iconv("UTF-8", "GBK", "400-000-9011"), $axis['x'], $axis['y'] + 200 + self::FONT_SIZE * 3);
        printer_draw_text($handle, iconv("UTF-8", "GBK", "购物小票为退/换货"), $axis['x'], $axis['y'] + 200 + self::FONT_SIZE * 4);
        printer_draw_text($handle, iconv("UTF-8", "GBK", "开具发票等有效凭证"), $axis['x'], $axis['y'] + 200 + self::FONT_SIZE * 5);
        printer_draw_text($handle, iconv("UTF-8", "GBK", "请妥善保管"), $axis['x'], $axis['y'] + 200 + self::FONT_SIZE * 6);
        printer_delete_font($size);
    }

    /**
     * 设置头部坐标
     *
     * @return array
     */
    public function setHeaderAxis()
    {
        return [
            'x' => self::WIDTH * 0.5,
            'y' => 30
        ];
    }

    /**
     * 设置局部坐标
     *
     * @return array
     */
    public function setMainAxis()
    {
        return [
            'x' => self::WIDTH * 0.5,
            'y' => 30
        ];
    }

    /**
     * 设置底部坐标
     *
     * @return array
     */
    public function setFooterAxis()
    {
        return [
            'x' => self::WIDTH * 0.5,
            'y' => 500
        ];
    }

    /**
     * 创建字体
     *
     * @param $fontHeight
     * @param $fontWidth
     * @param $fontWeight
     * @param string $font
     * @return mixed
     */
    public function generateFont($fontHeight, $fontWidth, $fontWeight, $font)
    {
        return printer_create_font($font, $fontHeight, $fontWidth, $fontWeight, false, false, false, 0);
    }

    /**
     * 设置字体
     *
     * @param float $ratio
     * @param $font
     * @return mixed
     */
    public function setFontSize($ratio = 0.33, $font = '宋体')
    {
        return $this->generateFont(self::FONT_SIZE, self::FONT_SIZE * $ratio, PRINTER_FW_NORMAL, $font);
    }
}